<!-- pages/pdf-preview/pdf-preview.vue -->
<template>
  <view class="speech-container">
    <!-- 语音输入界面 -->
    <view class="speech-input" v-if="!isRecognizing">
      <view 
        class="voice-button" 
        :class="{ recording: isRecording }"
        @touchstart="onTouchStart"
        @touchend="onTouchEnd"
        @touchcancel="onTouchCancel"
      >
        <view class="voice-icon">
          <text class="icon">🎤</text>
        </view>
        <text class="voice-text">
          {{ isRecording ? '松开结束' : '按住说话' }}
        </text>
      </view>
      
      <view class="voice-tips">
        <text>长按麦克风说话，最长60秒</text>
      </view>
    </view>
    
    <!-- 识别中界面 -->
    <view class="recognizing-view" v-else>
      <view class="loading-animation">
        <view 
          v-for="i in 3" 
          :key="i" 
          class="wave-bar"
          :style="getWaveStyle(i)"
        ></view>
      </view>
      <text class="recognizing-text">正在识别中...</text>
    </view>
    
    <!-- 识别结果 -->
    <view class="result-view" v-if="recognitionResult">
      <view class="result-header">
        <text class="result-title">识别结果</text>
        <text class="copy-btn" @click="copyText">复制</text>
      </view>
      <view class="result-content">
        <text class="result-text">{{ recognitionResult }}</text>
      </view>
    </view>
    
    <!-- 错误提示 -->
    <view class="error-view" v-if="errorMessage">
      <text class="error-text">{{ errorMessage }}</text>
      <text class="retry-btn" @click="retryRecognition">重试</text>
    </view>
  </view>
</template>

<script>
import SpeechManager from '@/common/utils/speechManager.js';
 
export default {
  data() {
    return {
      isRecording: false,
      isRecognizing: false,
      recognitionResult: '',
      errorMessage: '',
      touchTimer: null
    }
  },
  methods: {
    // 触摸开始
    async onTouchStart() {
      this.isRecording = true;
      this.errorMessage = '';
      
      // 开始录音
      try {
        await SpeechManager.startRecognition();
      } catch (error) {
		  console.log(error)
        this.handleError(error);
      }
    },
    
    // 触摸结束
    async onTouchEnd() {
      this.isRecording = false;
      this.isRecognizing = true;
      
      try {
        // 停止录音并识别
        const result = await SpeechManager.stopRecognition();
        this.recognitionResult = result;
        this.$emit('recognized', result);
      } catch (error) {
        this.handleError(error);
      } finally {
        this.isRecognizing = false;
      }
    },
    
    // 触摸取消
    onTouchCancel() {
      this.isRecording = false;
      this.isRecognizing = false;
      SpeechManager.cancelRecognition();
    },
    
    // 处理错误
    handleError(error) {
      console.error('语音识别错误:', error);
      this.errorMessage = this.getErrorMessage(error);
      this.isRecording = false;
      this.isRecognizing = false;
    },
    
    // 获取错误信息
    getErrorMessage(error) {
      const errorMap = {
        'network error': '网络连接失败',
        'not allowed': '没有录音权限',
        'no speech': '没有检测到语音',
        'audio capture': '录音设备错误',
        'default': '识别失败，请重试'
      };
      
      return errorMap[error.message] || errorMap['default'];
    },
    
    // 重试识别
    retryRecognition() {
      this.errorMessage = '';
      this.recognitionResult = '';
    },
    
    // 复制文本
    copyText() {
      uni.setClipboardData({
        data: this.recognitionResult,
        success: () => {
          uni.showToast({
            title: '复制成功',
            icon: 'success'
          });
        }
      });
    },
    
    // 获取波形样式
    getWaveStyle(index) {
      if (!this.isRecording) return {};
      
      const delay = (index - 1) * 0.2;
      return {
        animation: `wave 1s infinite ${delay}s`
      };
    }
  }
}
</script>

<style scoped>
.speech-container {
  padding: 40rpx;
}

.voice-button {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 200rpx;
  height: 200rpx;
  border-radius: 50%;
  background: linear-gradient(135deg, #007AFF, #5856D6);
  margin: 0 auto 40rpx;
  transition: all 0.3s;
}

.voice-button.recording {
  transform: scale(1.1);
  box-shadow: 0 10rpx 30rpx rgba(0, 122, 255, 0.3);
}

.voice-icon {
  margin-bottom: 20rpx;
}

.voice-icon .icon {
  font-size: 60rpx;
}

.voice-text {
  color: white;
  font-size: 28rpx;
}

.voice-tips {
  text-align: center;
  color: #999;
  font-size: 24rpx;
}

.recognizing-view {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 60rpx 0;
}

.loading-animation {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8rpx;
  margin-bottom: 30rpx;
}

.wave-bar {
  width: 8rpx;
  height: 30rpx;
  background-color: #007AFF;
  border-radius: 4rpx;
}

@keyframes wave {
  0%, 100% {
    transform: scaleY(1);
  }
  50% {
    transform: scaleY(2);
  }
}

.recognizing-text {
  color: #666;
  font-size: 28rpx;
}

.result-view {
  background: #F8F8F8;
  border-radius: 20rpx;
  padding: 30rpx;
  margin-top: 40rpx;
}

.result-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
}

.result-title {
  color: #666;
  font-size: 28rpx;
}

.copy-btn {
  color: #007AFF;
  font-size: 26rpx;
}

.result-content {
  background: white;
  border-radius: 10rpx;
  padding: 30rpx;
}

.result-text {
  font-size: 32rpx;
  line-height: 1.6;
  color: #333;
}

.error-view {
  background: #FFF2F2;
  border-radius: 20rpx;
  padding: 30rpx;
  margin-top: 40rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.error-text {
  color: #FF3B30;
  font-size: 28rpx;
  flex: 1;
}

.retry-btn {
  color: #007AFF;
  font-size: 28rpx;
  margin-left: 20rpx;
}
</style>

